/**
 * Document Module 
 *
 * Document module append, create , delete, 
 * search and do some other things connected 
 * to Node elements   
 *
 * @author Kuprienko Andrey  
 * @version 0.0.1
 */

var Document = function(){
	
};
/*
 * append(element,parent)
 * 
 * append element into parent
 * @param Element||string 'element'(function will make out)
 * @param Element 'parent'
 */
Document.prototype.append = function(element,parent){
	if(typeof element === 'string'){
		parent.innerHTML = element;
		return;
	}
	if((element.nodeType == 1) && (parent.nodeType == 1)){
		parent.appendChild(element);
	}else{
		return false;
	}
}

/** 
 * create_and_append_element(tag_name,attributes,parent,inner_html)
 *
 * create and append element into parent
 * set some attributes(including styles) if they are(!!!!) else ignore it
 * set 'parent' on 2 position and 'inner_html' on third one
 * and insert  some html there if it is set
 * @param string  'tag_name'
 * @param object 'attribures'
 * @param Element 'parent'
 * @param any 'inner_html'(set into string)
 * @return 'tag_name'  
 */
Document.prototype.create_and_append_element = function(tag_name,attributes,parent,inner_html){
	if(tag_name){
		if ((arguments.length == 2) && (attributes.nodeType == 1)) {
			parent = attributes;
			attributes = null;			
    	}
		if ((arguments.length == 3) && (attributes.nodeType == 1)) {
        	var change = parent;//send by value
			parent = attributes;
        	inner_html = change;
			attributes = null;
			change = null;			
    	}
		if(typeof tag_name === 'string'){
			var tag_name = document.createElement(tag_name);
		}
		if (attributes) {
        	for(var name in attributes){
				if(name === 'style'){
					for(var type in attributes[name]){
						tag_name.style.type = attributes.name.type;
					}
				}else{
		       		tag_name.setAttribute(name, attributes.name);
				}
			}
    	}
		if(inner_html){
			inner_html = String(inner_html);
			tag_name['innerHTML'] = inner_html;			
		}

		if((parent) && (parent.nodeType == 1)){
			parent.appendChild(tag_name);
			return tag_name;
		}else{
			return tag_name;
		}
		return tag_name;	
	}else{
		return false;
	}	

}

/*
 * set_element_inner_html(element,inner_html)
 *
 * set or create and set some inner html into element 
 * @param Element or String 'element'
 * @param String 'inner_html'
 * @return element(Element) 
 */
Document.prototype.set_element_inner_html = function(element,inner_html){
	if(typeof element === 'string'){
        var element = document.createElement(element);
		if(inner_html){
			element.innerHTML = inner_html;
		}
		return element;
	}else if((element.nodeType == 1) && inner_html){		
		element.innerHTML = inner_html;
		return element;
	}else{
		return false;
	}	
}

/*
 * get_elements_by_tag_name(tag_name,side_type)
 *
 * return elements or last/first element by tag_name
 * @param string 'tag_name'
 * @param string 'side_type'
 * set last or first element from array
 */
Document.prototype.get_elements_by_tag_name = function(tag_name,side_type){
	var elements = document.getElementsByTagName(tag_name);
	if(typeof tag_name === 'string'){ 
		if(!side_type){
			return elements;
		}else if(side_type === 'first'){
			return elements[0];
		}else if(side_type === 'last'){			
			return elements[elements.length-1];
		}else{
			return elements;
		}
	}else{
		return elements;
	}
}

/*
 * get_elements_by_name(name,side_type)
 *
 * return elements or last/first element by name
 * @param string 'name'
 * @param string 'side_type'
 * set last or first element from array
 */
Document.prototype.get_elements_by_name = function(name,side_type){
	var elements = document.getElementsByName(name);
	if(typeof name === 'string'){ 
		if(!side_type){
			return elements;
		}else if(side_type === 'first'){
			return elements[0];
		}else if(side_type === 'last'){			
			return elements[elements.length-1];
		}else{
			return elements;
		}
	}else{
		return elements;
	}
}

/*
 * get_element_by_id(id)
 *
 * return element by id
 * @param string 'id'
 */
Document.prototype.get_element_by_id = function(id){
	if(id){
		return document.getElementById(id);
	}else{
		return id;
	}
}

/*
 * get_element_children(element,tag_name,side_type)
 *
 * return element's children or last/first element's child by tag_name
 * @param object Element 'element' 
 * @param string 'tag_name'
 * use method 'getElementsByTagName()' of the object Element(not Document which has the same one) 
 * @param string 'side_type'
 * set last or first element from array
 */
Document.prototype.get_element_children = function(element,tag_name,side_type){
	if(element.nodeType == 1){
	    var children = element.getElementsByTagName(tag_name);
	}else{
		return;
	}
	if(element && (typeof tag_name === 'string')){
		if(!side_type){
			return children;
		}else if(side_type === 'first'){
			return children[0];
		}else if(side_type === 'last'){			
			return children[children.length-1];
		}else{
			return children;
		}
	}else{
		return children;
	}	
}

/*
 * get_element_defenite_descendants(element,tag_name)
 *
 * return element's descendants
 * when the next children brunch stops function makes the same thing  
 * with another brunch recursively till it find the wanted tag   
 * @param Element 'element' 
 * @param string 'tag_name' 
 * @return array of Node
 */
 
 //------???????????????doesnot go inside a table (in my case)?????????????????????
Document.prototype.get_element_defenite_descendants = function(element,tag_name){
	var tag_name = tag_name.toUpperCase();
	var descendants = new Array();
	for(var m = element.firstChild; m != null; m = m.nextSibling) {
        if(m.nodeName == tag_name){
			descendants.push(m);
			continue;
		}else if(m.nextSibling == null){
		    this.get_element_defenite_descendants(m,tag_name)
		}		
	}
	return descendants;
}
	
/*
 * get_element_inner_text(element)
 *
 * return element inner text
 * @param object Element 'element' 
 */
Document.prototype.get_element_inner_text = function(element){
	if(element){
		return element.innerText;
	}else{
		return element;
	}		
}

/*
 * get_element_inner_html(element)
 *
 * return element inner html
 * @param object Element 'element' 
 */
Document.prototype.get_element_inner_html = function(element){
	if(element){
		return element.innerHTML;
	}else{
		return element;
	}	
}

/*
 * remove_children(element)
 *
 * delete all children
 * @param object Element 'element'
*/
Document.prototype.remove_children = function(element){
	while(element.childNodes[0]){
  		element.removeChild(element.childNodes[0]);
	}

}
